﻿using ASmile.Com.DictCom;
using ASmile.DBModel;
using ASmile.Extend;
using System;
using System.Text;

namespace ASmile.Com.Da
{
    public class DictHelperBLL : BLLBase, IDictHelperBLL
    {
        /// <summary>
        /// 获取字典数据源
        /// </summary>
        public ApiResultList<Dict_Common> GetDicts(ArgDictSearch arg)
        {
            return
            GetPagerList<Dict_Common>(arg, query =>
            {
                if (arg.DictType != null)
                {
                    query.WhereAnd(s => s.DictType == arg.DictType);
                }
                if (arg.DictCode.IsNotEmpty())
                {
                    query.WhereAnd(s => s.DictCode == arg.DictCode);
                }
                if (arg.DictTyps?.Length > 0)
                {
                    query.WhereAnd(s => s.WEx_In(s.DictType, arg.DictTyps));
                }
                if (!arg.SearchVal.IsEmpty())
                {
                    string likeStr = arg.SearchValLike;
                    query.WhereAnd(s =>
                        s.WEx_Like(s.DictCode, likeStr) ||
                        s.WEx_Like(s.DictValue, likeStr) ||
                        s.WEx_Like(s.DictValuePY, likeStr)
                        );
                }
                query.OrderBy(s => s.DictValueOrderNo);
            });
        }
        /// <summary>
        /// 根据配置获取业务单据号
        /// EmBNoType
        /// </summary>
        /// <returns>返回单据号</returns>
        public ApiResult<string> GetBNoStr(int bNoType)
        {
            try
            {
                //如果这里出现并发卡顿的情况,应该将获取处方号放入缓存列表, 永远保持缓存中有20个处方号,获取时从缓存过去
                DB.TransStart(System.Data.IsolationLevel.ReadUncommitted);
                var model = DB.Query<Dict_BNo>(s => s.Id == bNoType).ToEntity();
                StringBuilder sb = new StringBuilder();
                sb.Append(model.FrontStr);
                if (!string.IsNullOrEmpty(model.DateFormat))
                {
                    if (model.LastDate.ToString(model.DateFormat) != DateTime.Now.ToString(model.DateFormat))
                    {
                        model.MaxValue = 0;
                    }
                    sb.Append(DateTime.Now.ToString(model.DateFormat));
                }
                model.MaxValue++;
                for (int i = 0; i < model.MaxValueLength - model.MaxValue.ToString().Length; i++)
                {
                    sb.Append("0");
                }

                sb.Append(model.MaxValue);
                sb.Append(model.BackStr);
                model.LastDate = DateTime.Now;
                DB.Update(model);
                DB.TransCommit();


                if (sb.Length == 0)
                {
                    return RetErr("ERR", "获取单号失败");
                }
                else
                {
                    return RetOK(sb.ToString(), "获取单号成功");
                }
            }
            catch (Exception ex)
            {
                DB.TransRollback();
                LogHelper.LogObj.Error($"获取BNo({bNoType})失败", ex);
                return RetErr("Exception", "获取单号异常");
            }
        }
        /// <summary>
        /// 获取区域列表
        /// </summary>
        public ApiResultList<Dict_Area> GetAreas(ArgGetAreas arg)
        {
            if (arg.PCode == null) arg.PCode = string.Empty;
            var query = CreateQuery<Dict_Area>(s => s.PCode == arg.PCode);
            if (arg.SearchVal.IsNotEmpty())
            {
                string likeStr = $"%{arg.SearchVal}%";
                query.WhereAnd(s =>
                    s.WEx_Like(s.AreaName, likeStr) ||
                    s.WEx_Like(s.AreaCode, likeStr) ||
                    s.WEx_Like(s.AreaNamePY, likeStr)

                    );
            }
            var rList = DB.Query<Dict_Area>(query).ToList();
            return RetOK(rList, 0);
        }
        /// <summary>
        /// 获取助记码
        /// </summary>
        public ApiResult<RetEasyCode> GetEasyCode(string word)
        {
            if (DataCacheCom.GetWordCode(word, out string py, out string wb))
            {
                RetEasyCode r = new RetEasyCode
                {
                    PY = py,
                    WB = wb
                };
                return RetOK(r, "OK");
            }
            return RetErr<RetEasyCode>("不存在");
        }
    }
}